package com.hc.conferencecheck.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hc.conferencecheck.entity.ResultMsg;
import com.hc.conferencecheck.entity.ResultStatusCode;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 用于过滤器拦截器中返回自定义信息的工具类
 */
public class CustomMessageUtil {
    /**
     * 返回自定义信息
     */
    public static void returnCustomMessage(HttpServletResponse response, ResultMsg resultMsg) {
        try {
            //设置返回数据类型
            response.setContentType("application/json");
            //如果不指定特定的编码集，返回的中文数据会乱码
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            ObjectMapper mapper = new ObjectMapper();
            //将返回结果转换为json，使用流对象输出
            String jsonString = JSON.toJSONString(resultMsg);
            out.print(jsonString);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
